package game.view.universal
{
	import com.greensock.TweenLite;
	import com.greensock.TweenMax;
	import com.greensock.easing.Linear;
	
	import flash.display.MovieClip;
	import flash.display.Shader;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.sensors.Accelerometer;
	import flash.text.TextField;
	
	import game.core.Angle;
	import game.core.LayerManager;
	import game.core.MCTagHelper;
	import game.core.Reflection;
	import game.core.ResLoadHelper;
	import game.core.StageProxy;
	import game.define.NotificationName;
	import game.define.typedef.AnimationMoveSpeed;
	import game.define.typedef.PlayerActionType;
	
	import org.puremvc.as3.patterns.facade.Facade;
	
	public class PlayerItem extends SceneItem
	{
		protected var _currAction:int;
		protected var _image:int;
		protected var _pointOffset:Point = new Point(0, 0); // 受心坐标偏移量
		protected var _nameText:TextField;
		
		public function PlayerItem()
		{
			super();
			
			_nameText = new TextField();
			_nameText.mouseEnabled = false;
			_nameText.textColor = 0xff0000;
			_nameText.x = -10;
			_nameText.y = 18;
			addChild(_nameText);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		protected function onEnterFrame(event:Event):void
		{
			cycleCurrAction();
		}
		
		private function cycleCurrAction():void
		{
			if (!_instance)
				return;
			
			var startTag:String = MCTagHelper.instance.getPlayerActionStartTag(_currAction);
			if (_instance.currentFrameLabel != null && 
				_instance.currentFrameLabel != startTag)
				playAction(_currAction);
		}
		
		public function playAction(action:int):void
		{
			_currAction = action;
			_playAction();
		}
		
		private function _playAction():void
		{return;
			if (!_instance)
				return;
			
			var mcTag:String = MCTagHelper.instance.getPlayerActionStartTag(_currAction);
			_instance.gotoAndPlay(mcTag);
		}
		
		public function setImage(image:int):void
		{
			if (_image == image)
				return;
			
			_image = image;
			
			ResLoadHelper.instance.loadPlayerImage(image, imageLoadComplete);
		}
		
		public function move(_x:int, _y:int, speed:Number = AnimationMoveSpeed.GENERAL):void
		{
			var p1:Point = new Point(this.x + _pointOffset.x, this.y + _pointOffset.y);
			var p2:Point = new Point(_x + _pointOffset.x, _y + _pointOffset.y);
			
			var distance:int = Point.distance(p1, p2);
			if (!distance)
				return;
			
			// 更新角色方向
			var angle:Number = Angle.getValue(p1, p2);
			if (
				(angle >= 351 && angle <= 360) || 
				(angle >= 0 && angle <= 10)
			){
				_currAction = PlayerActionType.RUN_LEFT;
			} else if (
				(angle >= 11 && angle <= 80)
			){
				_currAction = PlayerActionType.RUN_LEFT_UPPER;
			} else if (
				(angle >= 81 && angle <= 100)
			){
				_currAction = PlayerActionType.RUN_UPPER;
			} else if (
				(angle >= 101 && angle <= 170)
			){
				_currAction = PlayerActionType.RUN_RIGHT_UPPER;
			} else if (
				(angle >= 171 && angle <= 190)
			){
				_currAction = PlayerActionType.RUN_RIHT;
			} else if (
				(angle >= 191 && angle <= 260)
			){
				_currAction = PlayerActionType.RUN_RIGHT_LOWER;
			} else if (
				(angle >= 261 && angle <= 280)
			){
				_currAction = PlayerActionType.RUN_LOWER;
			} else if (
				(angle >= 281 && angle <= 350)
			){
				_currAction = PlayerActionType.RUN_LEFT_LOWER;
			}
			
			cycleCurrAction();
			
			var needTime:Number = distance * speed;
			TweenMax.to(this, needTime, {x:p2.x, y:p2.y, ease:Linear.easeNone, 
				onUpdate:moveProgress,
				onComplete:moveComplete});
		}
		
		private function moveProgress():void
		{
			Facade.getInstance().sendNotification(NotificationName.SCENE_PLAYER_MOVED, this);
		}
		
		private function moveComplete():void
		{
			switch (_currAction)
			{
				case PlayerActionType.RUN_LEFT:
					playAction(PlayerActionType.STAND_LEFT);
					break;
				case PlayerActionType.RUN_LEFT_LOWER:
					playAction(PlayerActionType.STAND_LEFT_LOWER);
					break;
				case PlayerActionType.RUN_LEFT_UPPER:
					playAction(PlayerActionType.STAND_LEFT_UPPER);
					break;
				case PlayerActionType.RUN_LOWER:
					playAction(PlayerActionType.STAND_LOWER);
					break;
				case PlayerActionType.RUN_RIGHT_LOWER:
					playAction(PlayerActionType.STAND_RIGHT_LOWER);
					break;
				case PlayerActionType.RUN_RIGHT_UPPER:
					playAction(PlayerActionType.STAND_RIGHT_UPPER);
					break;
				case PlayerActionType.RUN_RIHT:
					playAction(PlayerActionType.STAND_RIGHT);
					break;
				case PlayerActionType.RUN_UPPER:
					playAction(PlayerActionType.STAND_UPPER);
					break;
				default:
					break;
			}
		}
		
		public function setSide(side:int):void
		{
			
		}
		
		private function imageLoadComplete(data:*):void
		{
			if (_instance)
			{
				if (_instance.parent)
					_instance.parent.removeChild(_instance);
				_instance = null;
			}
			
//			_instance = Reflection.createInstance("MC_Player1");
			_instance = new MovieClip();
			var heart:Shape = new Shape();
			heart.graphics.beginFill(0xff0000);
			heart.graphics.drawCircle(0, 0, 20);
			heart.graphics.endFill();
			heart.x = -_pointOffset.x;
			heart.y = -_pointOffset.y;
			_instance.addChild(heart);
			addChild(_instance);
			
			_playAction();
		}
		
		public function showName(show:Boolean):void
		{
			_nameText.visible = show;
		}
		
		public function setName(data:String):void
		{
			_nameText.text = data;
		}
	}
}